<template>

  <div class="layui-layout layui-layout-admin">
    <div class="adminnav1">
      <Pagetop></Pagetop>
    </div>
    <div class="adminnav2">
      <Secondnav urlname="retail"></Secondnav>
    </div>

    <!-- 主体内容 -->
    <div class="layui-body kedou-admin" id="LAY_app_body app">

      <div class="layadmin-tabsbody-item layui-show">

        <div class="pagelist" style="padding:0">

          <div class="layui-tab layui-tab-card">
            <ul class="layui-tab-title">
              <li class="layui-this" @click='getdate("COMMODITY")'>商品奖金设置</li>
              <li @click='getdate("UNIFIED")'>统一奖金设置</li>
            </ul>
            <div class="layui-tab-content" style="padding:50px 0">
              <div class="layui-tab-item layui-show">
                <div class="pagetop">
                  <Search :seadate="searchArr"></Search>
                </div>
                <div class="layui-card-body">
                  <div class="buttop">
                    <button type="button" @click="addgoods()" class="layui-btn  layui-btn-normal">选择商品</button>
                  </div>
                  <Tables :fdate="pdate" :result="res" height="430" :totalCount="totalRows" :pageNumber="curindex"
                    :fbtn="btn"></Tables>
                </div>

              </div>
              <div class="layui-tab-item" style="font-size:14px">

                <div class="layui-form-item">
                  <label class="layui-form-label">一级奖金：</label>
                  <div class="layui-input-inline">
                    <input type="text" v-model="firstBonus" required lay-verify="required" placeholder="请输入数字"
                      autocomplete="off" class="layui-input">
                  </div>
                  <div class="layui-form-mid">%零售价</div>
                  <div class="layui-form-mid layui-word-aux">一级奖金即销售商品的推广家获得的奖金</div>
                </div>

                <div class="layui-form-item">
                  <label class="layui-form-label">二级奖金：</label>
                  <div class="layui-input-inline">
                    <input type="text" v-model="secondaryBonus" required lay-verify="required" placeholder="请输入数字"
                      autocomplete="off" class="layui-input">
                  </div>
                  <div class="layui-form-mid">%零售价</div>
                  <div class="layui-form-mid layui-word-aux">二级奖金即销售商品的推广家的上级获得奖金</div>
                </div>

                <div class="layui-form-item">
                  <label class="layui-form-label">创始奖金：</label>
                  <div class="layui-input-inline">
                    <input type="text" v-model="founderBonus" required lay-verify="required" placeholder="请输入数字"
                      autocomplete="off" class="layui-input">
                  </div>
                  <div class="layui-form-mid">%零售价</div>
                  <div class="layui-form-mid layui-word-aux">创始人奖金</div>
                </div>

                <div class="layui-form-item">
                  <div class="layui-input-block">
                    <button class="layui-btn" @click="save()" lay-submit lay-filter="formDemo">立即提交</button>
                  </div>
                </div>

              </div>
            </div>
          </div>

        </div>
      </div>
    </div>

  </div>
</template>
<script>
import Pagetop from '../common/pagetop.vue'
import Secondnav from '../common/secondnav.vue'
import Tables from '../common/tables.vue'
import Search from '../common/search.vue'
export default {
  name: 'memberlist',
  components: { Pagetop, Secondnav, Tables, Search },
  data () {
    return {
      pdate: [], //表头
      res: "", //内容
      totalRows: "",//总数量
      curindex: 1, //页数
      btn: [],//按钮
      searchArr: [],//搜索
      isShow: false,
      seapram: '',
      secondaryBonus: "",
      firstBonus: '',
      founderBonus: ""
    }
  },
  mounted () {
    layui.use('element', function () {
      var element = layui.element;
    })
    this.getdate('COMMODITY');
    this.getsearch();
  },
  methods: {
    getdate: function (type) {
      var _this = this;
      var url = "/retail/bapi/v1.9/share-bonus"
      if (type == 'UNIFIED') {
        url = url + "?settingType.in=UNIFIED&page=0"
        ajaxRequest(url, 'GET', 'json', "", function (ret, err) {
          if (ret.data.list.length > 0) {
            var res = ret.data.list[0];
            _this.secondaryBonus = res.secondaryBonus
            _this.firstBonus = res.firstBonus
            _this.founderBonus = res.founderBonus
          }
        })
      }
      else {
        if (_this.seapram) {
          var dates = {
            size: pageSize,
            page: _this.curindex,
            sort: 'createAt,DESC',
            'settingType.in': 'COMMODITY'
          }
          var res = _this.seapram;
          for (var i = 0; i < res.length; i++) {
            dates[res[i].key + '.contains'] = res[i].val;
          }

        } else {
          var dates = {
            size: pageSize,
            page: _this.curindex,
            sort: 'createAt,DESC',
            'settingType.in': 'COMMODITY'
          }
        }
        ajaxRequest(url, 'GET', 'json', dates, function (ret, err) {
          var arr = [];
          arr[0] = [
            { field: 'id', title: 'ID', }
            , { field: 'picture', title: '商品图片', templet: '<div><img src="{{d.picture}}" style="display: inline-block; height: 100%;"></div>' }
            , { field: 'commodityName', title: '商品名称', }
            , { field: 'price', title: '商品价格(元)', templet: function (d) { return (d.price / 100) } }
            , {              field: 'settingType', title: '设置奖金类型', templet: function (d) {
                if (d.settingType == 'COMMODITY') return '商品设置'
                else return '统一设置'
              }            }
            , { field: 'firstBonus', title: '一级奖金（%）' }
            , { field: 'secondaryBonus', title: '二级奖金（%）' }
            , { field: 'founderBonus', title: '创始奖金（%）' } 
            , {              field: 'right', title: '操作', fixed: 'right', templet: function (d) {
                var str = '<a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="edit">编辑</a>';
                return str;
              }            }
          ];
          _this.pdate = arr;
          _this.res = ret.data.list;
          _this.totalRows = ret.data.totalRows;
          _this.curindex = ret.data.page + 1;

          // alert(JSON.stringify(ret.data))
        })

      }
    },
    getsearch: function () {
      var arr = [
        { field: 'id', title: 'ID', type: 'input' }
      ];
      this.searchArr = arr;
    },
    page: function (index, obj) {
      //   alert(JSON, stringify(obj))
      this.curindex = index;
      this.getdate();

    },
    sousuo: function (pram) {
      //alert(JSON.stringify(pram))
      this.seapram = pram;
      this.res = '';
      this.totalRows = '';
      this.curindex = 1;
      this.getdate();
    },
    screens: function () {
      return screen();
    },
    save: function () {
      var url = '/retail/bapi/v1.9/share-bonus';
      var dates = {
        "firstBonus": this.firstBonus,
        "founderBonus": this.founderBonus,
        "secondaryBonus": this.secondaryBonus,
        "settingType": "UNIFIED",
      }
      ajaxRequest(url, 'POST', 'json', dates, function (ret, err) {
        layer.msg('设置成功');
      })
    },
    addgoods: function () {
      layer.open({
        type: 2
        , title: '选择需要推广的商品'
        , content: baseurl + '/tggoods'
        , area: ['90%', '80%']
      });
    },
    editInfo: function (data) {
      var _this = this;
      layer.open({
        type: 2
        , title: '编辑设置'
        , content: baseurl + '/tgedit'
        , area: ['90%', '80%']
        , success: function (layero, index) {
          var iframeWin = window[layero.find('iframe')[0]['name']];
          iframeWin.getdates(data);
        },
      });
    }
  },
  updated: function () {
    this.isShow = true;
    layui.use('form', function () {
      var form = layui.form;
      form.render();
    });
  },
}
</script>
<style scoped>
/**/
</style>
